package ie.timalb.chess.main.core.rules;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import ie.timalb.chess.main.core.Board;
import ie.timalb.chess.main.core.Turn;
import ie.timalb.chess.main.core.movement.Movement;
import ie.timalb.chess.main.core.rules.types.MovingInItsOwnTurnRule;
import ie.timalb.chess.main.core.rules.types.RouteNotOccupiedRule;
import ie.timalb.chess.main.core.rules.types.ValidMovementRule;

public class RuleSet {
	Collection <Rule> rules;
	
	public RuleSet() 
	{
		rules = new ArrayList<Rule> ();
		Rule movingInItsOwnTurn = new MovingInItsOwnTurnRule ();
		Rule validMovement = new ValidMovementRule ();
		Rule routeNotOccupied = new RouteNotOccupiedRule();		
		
		rules.add(validMovement);
		rules.add(routeNotOccupied);
		rules.add(movingInItsOwnTurn);
		
	}

	public boolean validateMovement(Turn turn, Movement movement, Board board) {
		Iterator <Rule> ruleIterator = this.getRules().iterator();
		Rule ruleToValidate;
		boolean ruleValidated;
		boolean ruleSetValidated = true;
		
		
		while (ruleIterator.hasNext())
		{
			ruleToValidate = ruleIterator.next();
			ruleValidated = ruleToValidate.validateMovement (turn, movement, board);
			if (!ruleValidated)
			{
				ruleSetValidated = false;
				break;
			}
		}		
		
		return (ruleSetValidated);
	}

	public Collection<Rule> getRules() {
		return rules;
	}

	public void setRules(Collection<Rule> rules) {
		this.rules = rules;
	}

}
